﻿using API.Logic;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RestSharp;
using System.Net;
using System.Reflection;

namespace API.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class CameraController : ControllerBase
    {
        private readonly CameraFunction _CameraFunction;

        public CameraController(CameraFunction cameraFunction)
        {
            this._CameraFunction = cameraFunction;
        }

        [HttpGet("{code}")]
        public JsonResult Get(string code,int channel=1)
        {
            try
            {
                Uri url = new Uri("https://open.ys7.com/api/lapp/v2/live/address/get");

                var client = new RestClient(url);

                var request = new RestRequest(url, Method.Post);

                var config = this._CameraFunction.CameraConfig();

                request.AddParameter("accessToken", config.AccessToken);

                request.AddParameter("deviceSerial", code.ToUpper().Trim());

                var response = client.Execute(request);

                //Console.WriteLine(response.Content);

                if (response.StatusCode == HttpStatusCode.OK && response.Content != null)
                {
                    var result = JsonConvert.DeserializeObject<dynamic>(response.Content);

                    if (result != null)
                    {
                        if (result.code == "200")
                        {
                            return new JsonResult(new { code = 200, data = result.data.url.Value, sign = config.AccessToken, msg = result.msg.Value });
                        }
                        else
                        {
                            return new JsonResult(new { code = Convert.ToInt32(result.code.Value), data = string.Empty,sign=string.Empty, msg = result.msg.Value });
                        }
                    }
                    else
                    {
                        return new JsonResult(new { code = -1, data = string.Empty, sign = string.Empty, msg = "系统错误" });
                    }
                }
                else
                {
                    return new JsonResult(new { code = -1, data = string.Empty, sign = string.Empty, msg = "系统错误" });
                }


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                return new JsonResult(new { code = -1, data = string.Empty, sign = string.Empty, msg = "系统错误" });
            }

        }

        [HttpGet("Status/{code}")]
        public JsonResult Status(string code)
        {
            try
            {
                Uri url = new Uri("https://open.ys7.com/api/lapp/device/info");

                var client = new RestClient(url);

                var request = new RestRequest(url, Method.Post);

                var config = this._CameraFunction.CameraConfig();

                request.AddParameter("accessToken", config.AccessToken);

                request.AddParameter("deviceSerial", code.ToUpper().Trim());

                var response = client.Execute(request);

                //Console.WriteLine(response.Content);

                if (response.StatusCode == HttpStatusCode.OK&& response.Content!=null)
                {
                    var result = JsonConvert.DeserializeObject<dynamic>(response.Content);

                    if(result != null)
                    {
                        if(result.code=="200")
                        {
                            return new JsonResult(new { code = 200, status = result.data.status.Value, msg = result.msg.Value });
                        }
                        else
                        {
                            return new JsonResult(new { code = Convert.ToInt32(result.code.Value), status = 0, msg = result.msg.Value });
                        }
                    }
                    else
                    {
                        return new JsonResult(new { code = -1, status = 0, msg= "系统错误" });
                    }
                }
                else
                {
                    return new JsonResult(new { code = -1, status = 0, msg = "系统错误" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                return new JsonResult(new { code=-1,status=0,msg="系统错误"});
            }

        }


    }
}
